package com.fowo.api.costAccounting.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    /**
     * 显示年月日时分秒，例如 2015-08-11 09:51:53.
     */
    public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    //根据时间获取对应 00：00：00
    public static Date getTimesBegin(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String startDay = df.format(date);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Integer.parseInt(startDay.substring(0,4)), Integer.parseInt(startDay.substring(5,7)) - 1, Integer.parseInt(startDay.substring(8,10)) );
        String firstDayOfMonth = new SimpleDateFormat( "yyyy-MM-dd ").format(calendar.getTime());
        firstDayOfMonth=firstDayOfMonth+" 00:00:00";
        SimpleDateFormat sdf1 = new SimpleDateFormat(DATETIME_FORMAT);
        Date parse = null;
        try {
            parse = sdf1.parse(firstDayOfMonth);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }
    //根据时间获取对应 00：00：00
    public static Date getTimesEnd(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String startDay = df.format(date);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Integer.parseInt(startDay.substring(0,4)), Integer.parseInt(startDay.substring(5,7)) - 1, Integer.parseInt(startDay.substring(8,10)) );
        String firstDayOfMonth = new SimpleDateFormat( "yyyy-MM-dd ").format(calendar.getTime());
        firstDayOfMonth=firstDayOfMonth+" 23:59:59";
        SimpleDateFormat sdf1 = new SimpleDateFormat(DATETIME_FORMAT);
        Date parse = null;
        try {
            parse = sdf1.parse(firstDayOfMonth);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }
    //根据时间获取对应 00：00：00
    public static String getTimesBeginStr(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String startDay = df.format(date);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Integer.parseInt(startDay.substring(0,4)), Integer.parseInt(startDay.substring(5,7)) - 1, Integer.parseInt(startDay.substring(8,10)) );
        String firstDayOfMonth = new SimpleDateFormat( "yyyy-MM-dd ").format(calendar.getTime());
        firstDayOfMonth=firstDayOfMonth+" 00:00:00";
        return firstDayOfMonth;
    }
    //根据时间获取对应 00：00：00
    public static String getTimesEndStr(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String startDay = df.format(date);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Integer.parseInt(startDay.substring(0,4)), Integer.parseInt(startDay.substring(5,7)) - 1, Integer.parseInt(startDay.substring(8,10)) );
        String firstDayOfMonth = new SimpleDateFormat( "yyyy-MM-dd ").format(calendar.getTime());
        firstDayOfMonth=firstDayOfMonth+" 23:59:59";
        return firstDayOfMonth;
    }
    public static String getLastMonth(String date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
        Date parse = null;
        try {
            parse = df.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
        calendar.setTime(parse);
        calendar.add(Calendar.MONTH, -1);//当前时间减去一个月
        return df.format(calendar.getTime());
    }
}
